Program ret_min_turnover
      character*6 ticker
      character*8 cusp,cusip
      integer(kind=8):: nshrs,nvalue,jvalue,ivol,numshrs(5500),ntot(5500),navail(5500),iperm(5500)
      dimension nep(5500),ntype(5500),jyr(5500),jmn(5500),jdy(5500),vwret(5500),retrn(5500),jdate(5500),tfact(5000),favg(5500),fret(5500)
      dimension pctavail(5500),ivol(5500),ishrout(5500),price(5500),turno(5500),aturn(1100)
      dimension aret(10),avgfee(10) 
      open(unit=10,file='e:\FIS_duration\episode_data_type_total_rep.txt')
      open(unit=99,file='e:\FIS_duration\ret_bef_aft_min_turn_rep.txt')
      
      iend=0
      i=0
1     i=i+1      
2     read(10,1000,end=900)nep(i),ntype(i),jast,jyr(i),jmn(i),jdy(i),cusp,ticker,itickt,numshrs(i),age,favg(i),fmax,fmin,fstd,iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util,navail(i),ntot(i)
1000  format(1x,i4,1x,i1,1x,i8,1x,i4,2i2,1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13,1x,i12) 
      if(ishrout(i).lt.1) go to 2
      turno(i)=float(ivol(i))/(1000*float(ishrout(i)))
      jdate(i)=jyr(i)*10000+jmn(i)*100+jdy(i)
      if(nep(i).eq.nep(1)) go to 1
5     icnt=i-1
      if(icnt.lt.30) go to 100
      rcnt=float(icnt)
      perd=.001+rcnt/5.
      nper=int(perd)
      ndif=icnt-5*nper
      aturn=0.
      do 20 n=1,nper
          ibg=(n-1)*5+ndif+1
          if(n.eq.1) ibg=1
          iend=n*5+ndif
          do 10 ii=ibg,iend
10             aturn(n)=aturn(n)+turno(ii)/float(iend-ibg+1)
!      write(6,6000)ibg,iend,aturn(n)
6000  format(1x,i4,1x,i4,1x,f10.6)  
20    continue 
      tmax=0.0
      tmin=999.9
      do 30 n=1,nper
          if(aturn(n).gt.tmax) max=n
          if(aturn(n).gt.tmax) tmax=aturn(n)
          if(aturn(n).lt.tmin) min=n
          if(aturn(n).lt.tmin) tmin=aturn(n)
30    continue
      rtbmax=1.
      rtamax=1.
      xsbmax=1.
      xsamax=1.
      rtbmin=1.
      rtamin=1.
      xsbmin=1.
      xsamin=1.
      ibg=1
      iend=max*5+ndif
      do 40 ii=ibg,iend
      xsbmax=xsbmax*(1.+retrn(ii)-vwret(ii))
40    rtbmax=rtbmax*(1.+retrn(ii)) 
      if(max.eq.nper) rtamax=-9.9
      if(max.eq.nper) xsamax=-9.9
      if(max.eq.nper) go to 52
      ibg=max*5+ndif+1
      iend=icnt
      do 50 ii=ibg,iend
      xsamax=xsamax*(1.+retrn(ii)-vwret(ii))    
50    rtamax=rtamax*(1.+retrn(ii))
      rtamax=rtamax-1.
      xsamax=xsamax-1.
52    rtbmax=rtbmax-1.
      xsbmax=xsbmax-1.
     
      ibg=1
      iend=min*5+ndif
      do 60 ii=ibg,iend
      xsbmin=xsbmin*(1.+retrn(ii)-vwret(ii))
60    rtbmin=rtbmin*(1.+retrn(ii)) 
      if(min.eq.nper) rtamin=-9.9
      if(min.eq.nper) go to 72
      ibg=min*5+ndif+1
      iend=icnt
      do 70 ii=ibg,iend
      xsamin=xsamin*(1.+retrn(ii)-vwret(ii))    
70    rtamin=rtamin*(1.+retrn(ii))
      rtamin=rtamin-1.
      xsamin=xsamin-1.
72    rtbmin=rtbmin-1.
      xsbmin=xsbmin-1.

      write(6,6001)nep(1),ntype(1),icnt,max,tmax,rtbmax,rtamax,xsbmax,xsamax,min,tmin,rtbmin,rtamin,xsbmin,xsamin
      write(99,6001)nep(1),ntype(1),icnt,max,tmax,rtbmax,rtamax,xsbmax,xsamax,min,tmin,rtbmin,rtamin,xsbmin,xsamin
6001  format(1x,i4,1x,i1,1x,i4,2(1x,i4,1x,f10.6,4(1x,f9.6)))      
      
100   continue
      nep(1)=nep(i)
      ntype(1)=ntype(i)
      jyr(1)=jyr(i)
      jmn(1)=jmn(i)
      jdy(1)=jdy(i)
      retrn(1)=retrn(i)
      vwret(1)=vwret(i)
      ntot(1)=ntot(i)
      numshrs(1)=numshrs(i)
      navail(1)=navail(i)
      price(1)=price(i)
      turno(1)=turno(i)
      ivol(1)=ivol(i)
      ishrout(1)=ishrout(i)
      favg(1)=favg(i)
      i=1
      go to 1
900   continue
      iend=iend+1
      if(iend.eq.1) go to 5
      stop
end program ret_min_turnover